<template>
  <a-modal
    title="编辑数据库信息表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="数据库id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据库id" v-decorator="['databaseId', {rules: [{required: true, message: '请输入数据库id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表权限分级"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表权限分级" v-decorator="['tablePermissions', {rules: [{required: true, message: '请输入表权限分级！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表中文名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表中文名称" v-decorator="['tableCnName', {rules: [{required: true, message: '请输入表中文名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表用户"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表用户" v-decorator="['tableSchema', {rules: [{required: true, message: '请输入表用户！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表类型" v-decorator="['tableType', {rules: [{required: true, message: '请输入表类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表备注" v-decorator="['tableRemarks', {rules: [{required: true, message: '请输入表备注！'}]}]" />
        </a-form-item>
        <a-form-item
          label="该库内表的总数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入该库内表的总数" v-decorator="['tableSum', {rules: [{required: true, message: '请输入该库内表的总数！'}]}]" />
        </a-form-item>
        <a-form-item
          label="该库内表内字段的总数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入该库内表内字段的总数" v-decorator="['tableFieldSum', {rules: [{required: true, message: '请输入该库内表内字段的总数！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表的创建时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表的创建时间" v-decorator="['tableCreateTime', {rules: [{required: true, message: '请输入表的创建时间！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { tableInfoEdit } from '@/api/modular/main/tableinfo/tableInfoManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              databaseId: record.databaseId,
              tablePermissions: record.tablePermissions,
              tableCnName: record.tableCnName,
              tableSchema: record.tableSchema,
              tableType: record.tableType,
              tableRemarks: record.tableRemarks,
              tableSum: record.tableSum,
              tableFieldSum: record.tableFieldSum,
              tableCreateTime: record.tableCreateTime
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            tableInfoEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
